package cn.qihang.ssm.controller;

import cn.qihang.ssm.pojo.Product;
import cn.qihang.ssm.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: qihang
 * @Date: 2022/10/31 14:36
 * @Desc:
 */
@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 查询所有产品
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("findAll")
    public ModelAndView findAll(
            @RequestParam(value = "page",defaultValue = "1") Integer page ,
            @RequestParam(value = "size" , defaultValue = "5") Integer size
    ){

        PageHelper.startPage(page,size);
        List<Product> productList = this.productService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(productList);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageInfo" , pageInfo);
        modelAndView.setViewName("product-list");
        return modelAndView;
    }

    /**
     * 新增产品
     * @param product
     * @return
     */
    @PostMapping("save")
    public String save(Product product){
        this.productService.save(product);
        return "redirect:findAll.do";
    }

    /**
     * 指定删除产品
     * @param id
     * @return
     */
    @GetMapping("deleteById")
    public String deleteById(@RequestParam(name = "id") Integer id){
        this.productService.deleteById(id);
        return "redirect:findAll.do";
    }


    /**
     * 修改产品前的数据回显
     * @param id
     * @return
     */
    @RequestMapping("editById")
    public ModelAndView editById(@RequestParam(name = "id") Integer id){
        ModelAndView modelAndView = new ModelAndView();
        Product product = productService.findById(id);
        modelAndView.addObject("product",product);
        modelAndView.setViewName("product-edit");
        return modelAndView;
    }


    /**
     * 修改产品
     * @param product
     * @return
     */
    @RequestMapping("edit")
    public String edit(Product product){
        this.productService.edit(product);
        return "redirect:findAll.do";
    }


    /**
     * 产品详情
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public ModelAndView findById(@RequestParam(name = "id") Integer id){
        ModelAndView modelAndView = new ModelAndView();
        Product product = productService.findById(id);
        modelAndView.addObject("product",product);
        modelAndView.setViewName("product-show");
        return modelAndView;
    }



    /**
     * 批量删除
     * @param request
     * @return
     */
    @RequestMapping("selectDelete")
    public String selectDelete(HttpServletRequest request){
        String ids = request.getParameter("ids");

        String[] split = ids.split(",");
        for (String s : split) {
            this.productService.deleteById(Integer.parseInt(s));
        }
        return "redirect:findAll.do";
    }
}
